<?php
	class InternationalMatch extends Model{
		
		private $_MatchId;
		private $_Country1;
		private $_Country2;
		private $_Result;
		private $_Round;
		private $_Place;
		private $_Surface;
		private $_Tournament;

		function __construct($arrInitialValues = null){
			parent::__construct();

			if (isset($arrInitialValues['surface']))
				$this->setSurface($arrInitialValues['surface']);
			else
				$this->setSurface('');
			
			if (isset($arrInitialValues['place']))
				$this->setPlace($arrInitialValues['place']);
			else
				$this->setPlace('');
			
			if (isset($arrInitialValues['match_id']))
				$this->setMatchId($arrInitialValues['match_id']);
			else
				$this->setMatchId(0);

			if (isset($arrInitialValues['country1']))
				$this->setCountry1($arrInitialValues['country1']);
			else
				$this->setCountry1(new Country());

			if (isset($arrInitialValues['country2']))
				$this->setCountry2($arrInitialValues['country2']);
			else
				$this->setCountry2(new Country());
				
			if (isset($arrInitialValues['result']))
				$this->setResult($arrInitialValues['result']);
			else
				$this->setResult(0);
				
			if (isset($arrInitialValues['round']))
				$this->setRound($arrInitialValues['round']);
			else
				$this->setRound(new Round());
				
			if (isset($arrInitialValues['tournament']))
				$this->setTournament($arrInitialValues['tournament']);
			else
				$this->setTournament(new Tournament());
		}
		
		public function setMatchId($value){
			$this->_MatchId = $value;
		}
		
		public function getMatchId(){
			return $this->_MatchId;
		}
		
		public function setPlace($value){
			$this->_Place = $value;
		}
		
		public function getPlace(){
			return $this->_Place;
		}
		
		public function setCountry1($value){
			$this->_Country1 = $value;
		}
		
		public function getCountry1(){
			return $this->_Country1;
		}

		public function setTournament($value){
			$this->_Tournament = $value;
		}
		
		public function getTournament(){
			return $this->_Tournament;
		}
		
		public function setCountry2($value){
			$this->_Country2 = $value;
		}
		
		public function getCountry2(){
			return $this->_Country2;
		}

		public function setResult($value){
			$this->_Result = $value;
		}
		
		public function getResult(){
			return $this->_Result;
		}

		public function getRound(){
			return $this->_Round;
		}
		
		public function setRound($value){
			$this->_Round = $value;
		}
		
		public function getSurface(){
			return $this->_Surface;
		}
		
		public function setSurface($value){
			$this->_Surface = $value;
		}

		public function deleteMatches(){
			$this->_delete('international_matches');
		}
		
		public function delete(){
			$this->_delete('serie');
		}
		
		private function _delete($table){
			$query = '
				DELETE
				FROM ' . $table . '
				WHERE serie_id = ' . $this->getMatchId();

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();	
		}

		public function getMatches(){
			$query = '
				SELECT *
				FROM matches
				WHERE match_id = ' . $this->getMatchId();
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function insert(){
			$query = '
				INSERT INTO serie
				(country1_id, country2_id, round_id, place, result, surface, tournament_id)
				VALUES
				(' . $this->getCountry1() . ', ' . $this->getCountry2() . ', "' . $this->getRound() . '", "' . 
				$this->getPlace() . '", "' . $this->getResult() . '", "'. $this->getSurface() . '", ' . $this->getTournament() . ')';

			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
		
		public function update(){
			$query = '
				UPDATE serie
				SET country1_id = ' . $this->getCountry1() . ',
					country2_id = ' . $this->getCountry2() . ',
					round_id = ' . $this->getRound() . ',
					place = "' . $this->getPlace() . '",
					result = "' . $this->getResult() . '",
					surface = "' . $this->getSurface() . '",
					tournament_id = ' . $this->getTournament() . '
				WHERE serie_id = ' . $this->getMatchId();

			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
		
		public function getAllMyMatches(){
			$query = '
				SELECT *
				FROM international_matches
				WHERE serie_id = ' . $this->getMatchId();

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
	}
?>
